
//** Haaf's Game Engine 1.8

//#include "global.h"

#include "game.h"


hgeSprite * tabs[5];
leftPanel * panel;
saveload * db;
hgeGUISlider *hslider;
hgeGUISlider *vslider;
hgeGUIEditBox * editBox;
hgeGUIButton *bNext;
hgeGUIButton *mSave;
hgeGUIButton *mLoad;
hgeGUIButton *bPrev;
hgeGUIButton *clear;

hgeGUIButton *GroundLayer;
hgeGUIButton *BackLayer;



char *  file_name;
HTEXTURE  textures[5];
//tile * tiles[4096];
tile * tiles[800][600];
	int GridX,GridY; 
float 	x;
float   y;
//////////////////////////////////////
bool init_mass(int _w,int _h)

{
	for(int i=0;i<_w;i++)
      for(int j=0;j<_h;j++)
	  {
       tiles[j][i] = new tile(false,-1,-1,-1,-1,-1,hge);
	  }

	return true;
}
/////////////////////////////////////
void drawTiles(int _w,int _h)
{
  for(int i=0;i<_w;i++)
      for(int j=0;j<_h;j++)
	  {
		  tiles[j][i]->drawTile(0,0);
	  }
}
////////////////////////////////////

void CheckTiles(int _w,int _h)
{
  for(int i=0;i<_w;i++)
      for(int j=0;j<_h;j++)
	  {
		  tiles[j][i]->checkVisibility(hslider->GetValue(),vslider->GetValue());
	  }
}

////////////////////////////////////
void LoadTextures()
{
    textures[0] = hge->Texture_Load("images/frame1.png");
	textures[1] = hge->Texture_Load("images/fence.png");
	textures[2] = hge->Texture_Load("images/scaffold.png");
	textures[3] = hge->Texture_Load("images/crates.png");
	textures[4] = hge->Texture_Load("images/grids_colors.png");
}
///////////////////////////////////////
bool aboveMouse(float _mouseX,float _mouseY,float tileSize)
{
	int mouseX,mouseY;

	 mouseX=_mouseX-160;
     mouseY=_mouseY;
	 GridX=(mouseX+((int)(hslider->GetValue()))*tileSize  )/tileSize   ;
	 GridY=(mouseY+(int)((vslider->GetValue()))*tileSize   )/tileSize   ;
	 
	 if((mouseX<0)||(mouseY<0))
	 {
       GridX=-1;
	   GridY=-1;
	 }


 return 0;
}
///////////////////////////////////////////////
bool FrameFunc()
{

	hge->Input_GetMousePos(&x,&y);
	float dt=hge->Timer_GetDelta();
	int id;
  if(hge->Input_GetKeyState(HGEK_ESCAPE)) {gui->Leave(); }	
	  id=gui->Update(dt);
	
	return false;
}


bool RenderFunc()
{
	// Render graphics
	hge->Gfx_BeginScene();
	Resources.GetSprite("spr_bg")->Render(0,0); 
	
	gui->Render();
	hge->Gfx_EndScene();

	return false;
}


int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	
	hge = hgeCreate(HGE_VERSION);

	hge->System_SetState(HGE_LOGFILE, "redactor.log");
	hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
	hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
	hge->System_SetState(HGE_TITLE, "redactor");
	hge->System_SetState(HGE_WINDOWED, true);
	hge->System_SetState(HGE_SCREENWIDTH, 800);
	hge->System_SetState(HGE_SCREENHEIGHT, 600);
	hge->System_SetState(HGE_SCREENBPP, 32);



	if(hge->System_Initiate())
	{	
		gui=new hgeGUI();
        panel=new leftPanel(hge);
        init_mass(100,100);
        db = new saveload();
		LoadTextures();
        
		gui->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
		gui->SetCursor(Resources.GetSprite("spr_cursor"));
		gui->SetFocus(1);
		gui->Enter();
		
		// Let's rock now!
		hge->System_Start();		
	}

	// Clean up and shutdown



	delete gui;
    Resources.Purge(0);
	hge->System_Shutdown();
	
	hge->Release();
	return 0;
}
